package com.yypx.common.struts;

import java.text.MessageFormat;
import java.util.ResourceBundle;

public class ResourceMsg {

	private static final String RESOURCENAME = "i18n.ApplicationMessages";

	private static ResourceMsg instance;

	private static ResourceBundle rb;

	public static ResourceMsg getInstance() {
		if (instance == null)
			instance = new ResourceMsg();
		if (rb == null)
			//rb = ResourceBundle.getBundle(RESOURCENAME,ActionContext.getContext().getLocale());
			rb = ResourceBundle.getBundle(RESOURCENAME);//到多语种切换时再考虑要不要换成上面一句
		return instance;
	}

	public String getResourceMsg(String key) {
		String result = null;
		if (rb == null)
			result = null;
		try {
			result = rb.getString(key);
			if (result == null)
				result = key;
			return result;
		} catch (Exception e) {
			return key;
		}
	}

	public String getResourceMsg(String key, String[] args) {
		String result = null;
		if (rb == null)
			result = null;
		try {
			result = rb.getString(key);
			if (result == null)
				result = key;
			if (args != null && args.length > 0) {
				MessageFormat mf = new MessageFormat(result);
				result = mf.format(args);
			}
			return result;
		} catch (Exception e) {
			return key;
		}
	}

}
